package com.xhsj.user.hr.system.web;

import com.xhsj.user.hr.system.entity.Dict;
import com.xhsj.user.hr.system.mapper.DictMapper;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 苏小林
 * @Description 缴费通知单表
 * @createTime 2020年02月20日 10:39:00
 */
@Slf4j
@Api(value = "字典表", description = "字典表")
@RequestMapping("/dict")
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class DictController {

    @Autowired
    private DictMapper dictMapper;


    /**
     * 通过type查询字典
     */
    @RequestMapping(value = "/findDictByType", method = RequestMethod.GET)
    @ApiOperation(value = "通过type查询字典", notes = "通过type查询字典")
    public Message findDictByType(@RequestParam("type") String type) {
        try {
            List<Map<String,Object>> list = dictMapper.findDictByType(type);
            return Message.success("成功",list);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return Message.error(1,e.getMessage());
        }
    }


    /**
     * 通过type集合查询字典
     */
    @RequestMapping(value = "/findDictByTypeList", method = RequestMethod.GET)
    @ApiOperation(value = "通过type查询字典", notes = "通过type查询字典")
    public Message findDictByTypeList(@RequestParam List<String> typeList) {
        try {
            List<Map<String,Object>> list = dictMapper.findDictByTypeList(typeList);
            return Message.success("成功",list);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return Message.error(1,e.getMessage());
        }
    }

    /**
     * 保存字典
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "保存字典", notes = "保存字典")
    public Message saveBill(@RequestBody Dict dict) {
        try {

            if (StringUtils.isBlank(dict.getId())) {
//                dict.setId(IdGen.generateUUID());
                dict.setCreateDate(new Date());
                dict.setCreateBy(new Long(1));
                dict.setUpdateDate(new Date());
                dict.setUpdateBy(new Long(1));
                dict.setDelFlag(0);
                dictMapper.insert(dict);
            } else {
                dict.setUpdateDate(new Date());
                dictMapper.updateById(dict);

            }
            return Message.success("创建成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error("创建失败!");
        }
    }

    /**
     * 通过type查询字典
     */
//    @RequestMapping(value = "/findDictByType", method = RequestMethod.GET)
//    @ApiOperation(value = "通过type查询字典", notes = "通过type查询字典")
//    public Message findDictByType(@RequestParam("type") String type) {
//        try {
//            List<Map<String,Object>> list = dictMapper.findDictByType(type);
//            return Message.success("成功",list);
//        } catch (NumberFormatException e) {
//            e.printStackTrace();
//            return Message.error(1,e.getMessage());
//        }
//    }

}
